  <template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent="">
          <el-col :span="6">
            <el-form-item label="设备名称">
              <el-input v-model="query.sbmc" placeholder="设备名称" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="设备种类">
              <el-select v-model="query.sblb" placeholder="设备种类" clearable>
                <el-option
                  v-for="(item, index) in sblbOptions"
                  :key="index"
                  :label="item.NAME"
                  :value="item.ID"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="设备类型">
              <el-select filterable v-model="query.sblx" placeholder="设备类型" clearable>
                <el-option
                  v-for="(item, index) in sblxOptions"
                  :key="index"
                  :label="item.NAME"
                  :value="item.ID"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <template v-if="showAll">
            <el-col :span="6">
              <el-form-item label="区域位置">
                <el-select
                  v-model="query.azwz"
                  placeholder="区域位置"
                  clearable
                >
                  <el-option
                    v-for="(item, index) in azwzOptions"
                    :key="index"
                    :label="item.MC"
                    :value="item.ID"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否特种设备">
                <el-select v-model="query.tzsb" placeholder="是否特种设备">
                  <el-option
                    v-for="(item, index) in tzsbOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </template>

          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
              <el-button
                type="text"
                icon="el-icon-arrow-down"
                @click="showAll = true"
                v-if="!showAll"
                >展开</el-button
              >
              <el-button
                type="text"
                icon="el-icon-arrow-up"
                @click="showAll = false"
                >收起</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
        <div class="JNPF-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
            >
              新增
            </el-button>
            <el-button
              type="text"
              icon="el-icon-download"
              @click="exportData()"
            >
              导出
            </el-button>
          </div>
          <div class="JNPF-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JNPF-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screenfull />
          </div>
        </div>
        <JNPF-table v-loading="listLoading" :data="list">
          <el-table-column prop="sbmc" label="设备名称" align="left" />
          <el-table-column prop="sbwh" label="设备位号" align="left" />
          <el-table-column label="设备类型" prop="sblx" algin="left">
            <template slot-scope="scope">{{
              scope.row.sblx | dynamicText(sblxOptions)
            }}</template>
          </el-table-column>
          <el-table-column label="设备种类" prop="sblb" algin="left">
            <template slot-scope="scope">{{
              scope.row.sblb | dynamicText(sblbOptions)
            }}</template>
          </el-table-column>
          <el-table-column label="区域位置" prop="azwz" algin="left">
            <template slot-scope="scope">{{
              scope.row.azwz | dynamicText(azwzOptions)
            }}</template>
          </el-table-column>
          <el-table-column prop="djrq" label="登记日期" align="left" />
          <el-table-column prop="djbm" label="登记部门" align="left" />
          <el-table-column label="设备状态" prop="sbzt" algin="left">
            <template slot-scope="scope">{{
              scope.row.sbzt | dynamicText(sbztOptions)
            }}</template>
          </el-table-column>
          <el-table-column label="是否特种设备" prop="tzsb" algin="left">
            <template slot-scope="scope">{{
              scope.row.tzsb | dynamicText(tzsbOptions)
            }}</template>
          </el-table-column>

          <el-table-column label="操作" fixed="right" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">
                编辑
              </el-button>
              <el-button
                type="text"
                @click="handleDel(scope.row.id)"
                class="JNPF-table-delBtn"
              >
                删除
              </el-button>
              <el-button
                type="text"
                @click="addOrUpdateHandle(scope.row.id, true)"
              >
                详情
              </el-button>
              <el-button type="text" @click="maintenanceRecord(scope.row.id)" >
								维护保养记录
							</el-button>
            </template>
          </el-table-column>
        </JNPF-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <!-- <JNPFForm v-if="formVisible" ref="JNPFForm" @refresh="refresh" /> -->
    <JNPFForm v-if="formVisible" ref="JNPFForm" @refresh="refresh" />
    <exportbox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
  </div>
</template> 
  <script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import JNPFForm from "./Form";
import ExportBox from "./ExportBox";
import { previewDataInterface } from "@/api/systemData/dataInterface";

export default {
  components: { JNPFForm, ExportBox },
  data() {
    return {
      showAll: false,
      query: {
        sbmc: undefined,
        sblb: undefined,
        sblx: undefined,
        azwz: undefined,
        tzsb: undefined,
      },
      defaultProps: {
        children: "children",
        label: "fullName",
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,

        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "sbmc", label: "设备名称" },
        { prop: "sbwh", label: "设备位号" },
        { prop: "sblx", label: "设备类型" },
        { prop: "sblb", label: "设备种类" },
        { prop: "azwz", label: "区域位置" },
        { prop: "djrq", label: "登记日期" },
        { prop: "djbm", label: "登记部门" },
        { prop: "sbzt", label: "设备状态" },
        { prop: "tzsb", label: "是否特种设备" },
      ],
      sblbOptions: [],
      sblxOptions: [],
      azwzOptions: [],
      sbztOptions: [{ fullName: "启用", id: "3" }],
      tzsbOptions: [
        { fullName: "是", id: "0" },
        { fullName: "否", id: "1" },
      ],
    };
  },
  created() {
    this.initData();
    this.getSblbOptions();
    this.getSblxOptions();
    this.getAzwzOptions();
  },
  methods: {
    // 维护保养记录
		maintenanceRecord() {},
    getSblbOptions() {
      previewDataInterface("000691b2b6ff47e182460b08ef9f0027").then((res) => {
        this.sblbOptions = res.data;
      });
    },
    getSblxOptions() {
      previewDataInterface("ecbdc678f2ee4ec68052d31ff0cf5a69").then((res) => {
        this.sblxOptions = res.data;
      });
    },
    getAzwzOptions() {
      previewDataInterface("e7b2c31f5a7147c08629c5409dd7fdf4").then((res) => {
        this.azwzOptions = res.data;
      });
    },

    initData() {
      this.listLoading = true;
      let query = {
        ...this.listQuery,
        ...this.query,
      };
      request({
        url: `/api/System/QTSBSS`,
        method: "get",
        data: query,
      }).then((res) => {
        this.list = res.data.list;
        this.total = res.data.pagination.total;
        this.listLoading = false;
        this.$nextTick(() => {
          this.tableHeight = "100%";
        });
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/System/QTSBSS/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JNPFForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query };
      request({
        url: `/api/System/QTSBSS/Actions/Export`,
        method: "POST",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        window.location.href = this.define.comUrl + res.data.url;
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,

        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,

        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
  },
};
</script>